/**
 * This file is part of Idegen, the application to create IDE project files.
 *
 * @copyright Copyright (c) 2011-2012, Walter Tamboer http://waltertamboer.nl
 * @file Idegen/Idegen/IdegenProject.cpp
 */

#include "Idegen/PrecompiledHeader.hpp"
#include "Idegen/Application/Settings.hpp"
#include "Idegen/Idegen/IdegenProject.hpp"
#include "Idegen/Idegen/IdegenProjectType.hpp"
#include "Idegen/Idegen/IdegenProjectFilter.hpp"
#include "Idegen/Idegen/IdegenProjectConfig.hpp"
#include "Idegen/Idegen/IdegenSourceSet.hpp"

namespace Idegen
{
	IdegenProject::IdegenProject()
	{
	}

	IdegenProject::~IdegenProject()
	{
		for ( IdegenProjectConfigList::iterator it = m_Configs.begin(); it != m_Configs.end(); ++it )
		{
			delete (*it);
		}
	}

	std::string IdegenProject::GetName() const
	{
		return m_Name;
	}

	std::string IdegenProject::GetOutputPath() const
	{
		return m_OutputPath;
	}

	IdegenProjectConfigList IdegenProject::GetConfigs() const
	{
		return m_Configs;
	}

	IdegenSourceSetList IdegenProject::GetSources() const
	{
		return m_Sources;
	}
	
	IdegenProjectType IdegenProject::GetType() const
	{
		return m_Type;
	}

	void IdegenProject::ParseAttributes( Settings* settings, TiXmlElement* parent )
	{
		ParseAttributeString( parent, "name", m_Name );
		m_Name = settings->ParseVariables( m_Name );
		
		std::string type;
		ParseAttributeString( parent, "type", type );
		m_Type = StringToProjectType( settings->ParseVariables( type ) );

		ParseAttributeString( parent, "output", m_OutputPath );
		m_OutputPath = settings->ParseVariables( m_OutputPath );
	}

	void IdegenProject::ParseConfiguration( Settings* settings, TiXmlElement* parent )
	{
		SettingsScope* scope = settings->GetScope();
		scope->Config = new IdegenProjectConfig();
		scope->Config->Unserialize( settings, parent );

		m_Configs.push_back( scope->Config );
	}

	void IdegenProject::ParseSource( Settings* settings, TiXmlElement* element )
	{
		IdegenSourceSet* sourceSet = NULL;
	
		std::string name;	
		if ( ParseAttributeString( element, "name", name ) )
		{
			name = settings->ParseVariables( name );
			sourceSet = settings->ParseSourceSet( name );

			// Try to find the element in one of the parents:
			if ( sourceSet == NULL )
			{
				TiXmlElement* parent = element->Parent()->ToElement();
				while ( parent != NULL && sourceSet == NULL )
				{
					sourceSet = settings->ParseSourceSet( name, parent );
					parent = parent->Parent()->ToElement();
				}
			}
		}
		
		// Check if it is an inline source:
		if ( sourceSet == NULL )
		{
			TiXmlElement* child = element->FirstChildElement();
			if ( child != NULL )
			{
				sourceSet = settings->ParseSourceSet( element );
			}
		}

		if ( sourceSet != NULL )
		{
			m_Sources.push_back( sourceSet );
		}
	}

	void IdegenProject::Unserialize( Settings* settings, TiXmlElement* parent )
	{
		ParseAttributes( settings, parent );

		TiXmlElement* element = parent->FirstChildElement();
		while ( element != NULL )
		{
			const std::string elementName = element->Value();
			
			if ( elementName == "i:config" )
			{
				ParseConfiguration( settings, element );
			}
			else if ( elementName == "i:source" )
			{
				ParseSource( settings, element );
			}

			element = element->NextSiblingElement();
		}
	}
}
